package patient

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func GetAddressList(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusOK, gin.H{
			"code": 401,
			"msg":  "用户未登录",
		})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetAddressList(ctx, &proto.GetAddressListRequest{
		UserId: userId.(int64),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "获取地址列表失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     200,
		"msg":      resp.Message,
		"addresses": resp.Addresses,
	})
}

func AddAddress(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusOK, gin.H{
			"code": 401,
			"msg":  "用户未登录",
		})
		return
	}

	var form request.AddAddressReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "请求参数错误",
		})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.AddAddress(ctx, &proto.AddAddressRequest{
		UserId:        userId.(int64),
		Province:      form.Province,
		City:          form.City,
		District:      form.District,
		Address:       form.Address,
		ReceiverName:  form.ReceiverName,
		ReceiverPhone: form.ReceiverPhone,
		IsDefault:     form.IsDefault,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "添加地址失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

func UpdateAddress(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusOK, gin.H{
			"code": 401,
			"msg":  "用户未登录",
		})
		return
	}

	var form request.UpdateAddressReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "请求参数错误",
		})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.UpdateAddress(ctx, &proto.UpdateAddressRequest{
		AddressId:     int64(form.AddressId),
		UserId:        userId.(int64),
		Province:      form.Province,
		City:          form.City,
		District:      form.District,
		Address:       form.Address,
		ReceiverName:  form.ReceiverName,
		ReceiverPhone: form.ReceiverPhone,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "更新地址失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

func DeleteAddress(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusOK, gin.H{
			"code": 401,
			"msg":  "用户未登录",
		})
		return
	}

	var form request.DeleteAddressReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "请求参数错误",
		})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.DeleteAddress(ctx, &proto.DeleteAddressRequest{
		AddressId: int64(form.AddressId),
		UserId:    userId.(int64),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "删除地址失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

func SetDefaultAddress(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusOK, gin.H{
			"code": 401,
			"msg":  "用户未登录",
		})
		return
	}

	var form request.SetDefaultAddressReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "请求参数错误",
		})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.SetDefaultAddress(ctx, &proto.SetDefaultAddressRequest{
		AddressId: int64(form.AddressId),
		UserId:    userId.(int64),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "设置默认地址失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}
